package com.exam.scjp5.objectives.section_1;

public class ExampleArray
{
    public static void main(String args[])
    {
        int[][][][] d4 = new int[10][5][5][5];
        int[][][] d3 = new int[10][12][12];
        int[][] d2 = new int[20][];
        
        d4[0]= d3; // Here d4[0] will be from d4[0][0][0][0] to d4[0][9][11][11];
        d3[0] = d2; // Now d3[0] room can handle only [20][]
        
        // OK! because d4[0] is pointing to d3 and d3[6] has 12 slots.
        // If we change the following to d4[0][0][0][0], then NullPointerException will be thrown,
        // because d3[0] is overwritten by d2 and d2 has not initialized its 2nd array.
        d4[0][6][0][0] = 10;
        d4[0][9][11][11] = 10;
        
        // ERROR! ArrayIndexOutOfBoundsException will occur because 'd4' is decalred [10][5][5][5]. 
        // But 'd4[0]' is overwritten by 'd3' which is [10][12][12]. So, d4[0] can accept up to [9][11][11].
        // However, the rest rooms of 'd4' can still accept up to [4][4][4].
        d4[1][3][6][6] = 11;
        
        // Now re-create array to d2
        d2 = new int[20][20];
        // Follow is still error, because d2 is now pointing to totally new different array.
        // But d4[0][0][1][1] is still old array.
        // It will be ok if you do like this d4[0][0][1] = new int[20];
        d4[0][0][1] = new int[20];
        d4[0][0][1][1] = 100;
        
        // The following is possible. Array is also subclass of 'Object'.
        // When you want to reuse, you need to cast obj back to int array.
        Object obj = d4;        
        System.out.println(((int[][][][])obj)[0][6][0][0]);
        
        System.out.println("d4[0][6][0][0] is " + d4[0][6][0][0]);
        System.out.println("d3[6][0][0] is " + d3[6][0][0]);
        
    }
}
